package com.example.healthsystem.config;

import com.example.healthsystem.model.vo.Result;
import com.example.healthsystem.util.JacksonUtils;
import com.example.healthsystem.util.JwtUtils;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.AuthorityUtils;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.stream.Collectors;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.web.filter.GenericFilterBean;

//public class JwtFilter extends GenericFilterBean {
//
//    @Override
//    public void doFilter(ServletRequest servletRequest,
//                            ServletResponse servletResponse,
//                            FilterChain chain) throws IOException, ServletException{
//
////        String header=request.getHeader("Authorization");
//
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//
//        String jwt=request.getHeader("Authorization");
//
//        if(JwtUtils.judgeTokenIsExist(jwt)){
//            try{
//                Claims claims=JwtUtils.getTokenBody(jwt);
//                String username=claims.getSubject();
//                List<GrantedAuthority> authorities= AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get("authorities"));
//
//                UsernamePasswordAuthenticationToken token=new UsernamePasswordAuthenticationToken(username,null,authorities);
//                SecurityContextHolder.getContext().setAuthentication(token);
//            }catch(Exception e){
//                e.printStackTrace();
//                response.setContentType("application/json;charset=utf-8");
//                Result result = Result.create(403, "凭证已失效，请重新登录！");
//                PrintWriter out = response.getWriter();
//                out.write(JacksonUtils.writeValueAsString(result));
//                out.flush();
//                out.close();
//                return;
//            }
//        }
//        chain.doFilter(servletRequest,servletResponse);
//    }
//
//}
public class JwtFilter{

}
